#pragma once

#include <iostream>
#include <string>
#include <unordered_map>
#include <fstream>
#include "Log.hpp"

const static std::string sep = ": ";

class Dict
{
private:
    void Load(const std::string& path)
    {
        std::ifstream in(path);
        if(!in.is_open())
        {
            LOG(FATAL,"Dict File Load error\n");
            exit(1);
        }
        std::string line;
        while(std::getline(in, line))
        {
            if(line.empty())
                continue ;
            LOG(DEBUG, "load info: %s , success\n", line.c_str());
            size_t pos = line.find(sep.c_str());
            if(pos == std::string::npos)
                continue ;
            std::string key = line.substr(0,pos);
            if(key.empty())
                continue;
            std::string val = line.substr(pos+sep.size());
            if(val.empty())
                continue;
            _map.emplace(key,val);
        }
        LOG(INFO, "load %s done\n", path.c_str());
        in.close();
    }
    
public:
    Dict(const std::string path)
    :_path(path)
    {
        Load(_path);
    }

    ~Dict()
    {}

    std::string Translate(std::string word)
    {
        if(word.empty())
            return "None";
        
        auto it = _map.find(word);
        if(it == _map.end())
            return "None";
        return it->second;
    }

private:
    std::string _path;
    std::unordered_map<std::string,std::string> _map;
};